package springBoot_dzfpxt.web.controller;

import java.util.Base64.Encoder;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import springBoot_dzfpxt.custom.CaozuoUtils;
import springBoot_dzfpxt.custom.JqGridParam;
import springBoot_dzfpxt.custom.UserUtils;
import springBoot_dzfpxt.custom.jqgridUtils;
import springBoot_dzfpxt.entity.Boos;
import springBoot_dzfpxt.entity.EimUser;
import springBoot_dzfpxt.entity.QiyecaozuoTable;
import springBoot_dzfpxt.entity.QuanxianTable;
import springBoot_dzfpxt.service.BoosService;
import springBoot_dzfpxt.service.QiyecaozuoTableService;
import springBoot_dzfpxt.service.QuanxianTableService;
import springBoot_dzfpxt.service.SuperBoosService;
import springBoot_dzfpxt.web.form.superboospage.BoosForm;
import springBoot_dzfpxt.web.form.superboospage.EditqxForm;

@Controller
@RequestMapping(value="/superbosswy")
public class SuperBoosController {
	
	@Autowired
	private BoosService boosService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private QuanxianTableService quanxianTableService;
	@Autowired
	private QiyecaozuoTableService caozuoService;
	@Autowired
	private SuperBoosService superBoosService;
	
	@RequestMapping(value="cxzhxx")
	public String cxzhxx() {
		return "superbosswy/zhgl/cxzh";
	}

	@RequestMapping(value="/cxzh/list")
	@ResponseBody
	public Object list(JqGridParam param) {
		//规定分页排序规则
		Pageable pageable = param.buildPageable();
		Page<QuanxianTable> page = null; 
		//判断param是否为空，为空则为是筛选查找
		if(!param.getSearch()) {
			//根据分页规则查询所有进行分页
			page = quanxianTableService.findAll(pageable);
		}else {
			//根据筛选规则和分页规则查找所有 进行先筛选查找再排序
			page = quanxianTableService.findAll(param.buildSpec(),pageable);
			}
		//从数据库查到以后打包封装有用的数据传到前端
		HashMap<String, Object> result = jqgridUtils.getPageResult(page);
		//返回前端需要的数据
		return result;
	}
	
	@RequestMapping(value="/editqx")
	public String editquanxian() {
		return "superbosswy/zhgl/editquanxian";
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(String id, EditqxForm form) {
		//获取编辑类型字符串
		String oper = form.getOper();
		//判断是增，删还是改
		if("del".equals(oper)) {
			quanxianTableService.deleteById(Integer.parseInt(id));
		}
		else if("add".equals(oper)) {
			QuanxianTable table = new QuanxianTable();
			BeanUtils.copyProperties(form, table);
			quanxianTableService.save(table);
		}
		
		else {
			QuanxianTable qx = quanxianTableService.findById(Integer.parseInt(id));
			QuanxianTable table = new QuanxianTable();
			//bean工具类里面的一个方法复制相同变量内容到另一个方法 赋值顺序 前赋值后
			BeanUtils.copyProperties(form, table);
			Boos boos = qx.getBoos();
			boos.setName(form.getBoos().getName());
			boos.setDanwei(form.getBoos().getDanwei());
			table.setBoos(boos);
			table.setId(qx.getId());
			quanxianTableService.save(table);
		}
		return "ok!";
	}
	
	@RequestMapping(value="/glzhrz")
	public String glzhrz() {
		return "superbosswy/yhczrz";
	}
	
	@RequestMapping(value="/tjgn")
	public String tjgn() {
		return "superbosswy/tjgn";
	}
	
	@RequestMapping(value="/czrz/list")
	@ResponseBody
	public Object czrzlist(JqGridParam param) {
		//规定分页排序规则
		Pageable pageable = param.buildPageable();
		Page<QiyecaozuoTable> page = null; 
		//判断param是否为空，为空则为是筛选查找
		if(!param.getSearch()) {
			//根据分页规则查询所有进行分页
			page = caozuoService.findAll(pageable);
		}else {
			//根据筛选规则和分页规则查找所有 进行先筛选查找再排序
			page = caozuoService.findAll(param.buildSpec(),pageable);
		}
		//从数据库查到以后打包封装有用的数据传到前端
		HashMap<String, Object> result = jqgridUtils.getPageResult(page);
		//返回前端需要的数据
		return result;
	}
	
	
	@RequestMapping(value="tjgn/list")
	@ResponseBody
	public Object tjgnlist(JqGridParam param) {
		//规定分页排序规则
		Pageable pageable = param.buildPageable();
		Page<Boos> page = null; 
		//判断param是否为空，为空则为是筛选查找
		if(!param.getSearch()) {
			//根据分页规则查询所有进行分页
			page = boosService.findAll(pageable);
		}else {
			//根据筛选规则和分页规则查找所有 进行先筛选查找再排序
			page = boosService.findAll(param.buildSpec(),pageable);
		}
		//从数据库查到以后打包封装有用的数据传到前端
		HashMap<String, Object> result = jqgridUtils.getPageResult(page);
		//返回前端需要的数据
		return result;
	}
	
	@RequestMapping(value="/tzxgmm")
	public String tzxgmm() {
		return "superbosswy/xgmm";
	}
	
	@RequestMapping(value="/jiaoyanpwd")
	@ResponseBody
	public Object jiaoyanpwd(String pwd) {
		String password = userUtils.getUser().getPassword();
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
		boolean matches = encoder.matches(pwd, password);	//这个方法可以校验前端用户输入的密码是否正确 ***参数前后顺序不能改变 1.前端输入密码 2.数据库保存密码***
		System.out.println(pwd);
		System.out.println(password);
		System.out.println(matches);
		return matches;
	}
	
	@RequestMapping(value="/tjmm")
	public String tjmm(String newpwd , String newpwd1) {
		System.out.println("新密码："+newpwd);
		System.out.println("二次新密码"+newpwd1);
		if(newpwd.length()<6||!newpwd.equals(newpwd1)) {
			return "redirect:tzxgmm";
		}else if(newpwd.equals(newpwd1)){
			EimUser user = userUtils.getUser();
			user.setPassword(new BCryptPasswordEncoder().encode(newpwd));
			superBoosService.save(user);
		}
		return "redirect:../beforelogout";
	}
}
